<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="search"></el-input>
      <el-button @click="claenSearch">清除</el-button>
      <el-button type="primary" @click="searchTask">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="form">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="updateDialog(row.id)">编辑</el-button>
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delTask(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger" >删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination layout="prev, pager, next,total" :current-page="params._page" :page-size="params._limit" :total="total" @current-change="handleCurrentChange" @size-change="handleSizeChange"> </el-pagination>
      <el-button size="small" plain @click="openDialog">+ 新增</el-button>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      @open="openDialog"
      @close="closeDialog"
      width="30%">
        <span>
          <el-form ref="form" :model="addlist" label-width="80px" :rules="rules">
            <el-form-item label="任务名称" prop="name">
              <el-input v-model="addlist.name"></el-input>
            </el-form-item>
            <el-form-item label="任务描述" prop="des">
              <el-input v-model="addlist.des"></el-input>
            </el-form-item>
          </el-form>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import { v4 as uuidv4 } from 'uuid'
import { fetchTaskList, delTaskList, addTaskList, showTaskList, updateTaskList, searchTaskList } from '@/api/task'
export default {
  data () {
    return {
      search: '',
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      total: null,
      form: [],
      dialogVisible: false,
      addlist: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{
          required: true,
          message: '任务名称必须填写',
          trigger: ['blur']
        }],
        des: [{
          required: true,
          message: '任务描述必须填写',
          trigger: ['blur']
        }]
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    async getList () {
      const res = await fetchTaskList(this.params)
      // console.log(res)
      this.form = res.data
      this.total = Number(res.headers['x-total-count'])
    },
    openDialog () {
      this.dialogVisible = true
    },
    closeDialog () {
      this.dialogVisible = false
    },
    handleCurrentChange (page) {
      this.params._page = page
      this.getList()
    },
    handleSizeChange (size) {
      this.params._limit = size
      this.getList()
    },
    async delTask (id) {
      console.log(id)
      await delTaskList(id)
      this.getList()
    },
    confirm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.addlist.id) {
            await updateTaskList(this.addlist)
            this.$message.success('修改成功')
          } else {
            this.addlist.id = uuidv4()
            await addTaskList(this.addlist)
            this.$message.success('新增成功')
          }
          this.getList()
          this.dialogVisible = false
          this.addlist = {
            name: '',
            des: '',
            id: ''
          }
        }
      })
    },
    async updateDialog (id) {
      this.dialogVisible = true
      const res = await showTaskList(id)
      // console.log(res)
      this.addlist = res.data
    },
    async searchTask () {
      const res = await searchTaskList(this.search)
      console.log(res)
      this.form = res.data
      this.total = 1
    },
    claenSearch () {
      this.search = ''
      this.getList()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
